package com.zhwmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhwmall.common.entity.product.AttrEntity;
import com.zhwmall.common.entity.product.AttrGroupEntity;
import com.zhwmall.common.utils.R;
import com.zhwmall.common.vo.AttrGroupWithAttrVo;
import com.zhwmall.product.service.AttrGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author zhw
 * @email sqzhou@88.com
 * @date 2020-08-18 16:52:35
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId){

        List<AttrGroupWithAttrVo> vos = attrGroupService.getAttrGroupWithAttrByCatelogId(catelogId);

        return R.ok("success").put("data", vos);
    }

    //获取关联属性
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities =  attrGroupService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    // 获取没有关联属性
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noAttrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId){
        IPage<AttrEntity> entities =  attrGroupService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("data",entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable(value = "catId") long catId){
        IPage<AttrGroupEntity> pageList = attrGroupService.pageList(params, catId);
        return R.ok().put("data", pageList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] value = attrGroupService.findParent(attrGroup.getCatelogId());
        attrGroup.setValuePath(value);
        return R.ok().put("data", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

}
